package org.example.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.admin.dto.RolePermissionDto;
import org.example.admin.service.AdminPermissionService;
import org.example.admin.service.AdminRolePermissionService;
import org.example.admin.service.AdminRoleService;
import org.example.admin.service.AdminUserRoleService;
import org.example.common.constant.ResponseCode;
import org.example.common.utils.ResponseUtil;
import org.example.db.entity.Permission;
import org.example.db.entity.Role;
import org.example.db.entity.RolePermission;
import org.example.db.entity.UserRole;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "系统管理-角色管理")
@RestController
@Slf4j
@RequestMapping("/sys/role")
public class AdminSystemRoleController {


    AdminRoleService adminRoleService;
    AdminPermissionService adminPermissionService;
    AdminRolePermissionService adminRolePermissionService;
    RedisTemplate<String, Object> redisTemplate;
    AdminUserRoleService adminUserRoleService;

    public AdminSystemRoleController(AdminRoleService adminRoleService, AdminPermissionService adminPermissionService, AdminRolePermissionService adminRolePermissionService, RedisTemplate<String, Object> redisTemplate, AdminUserRoleService adminUserRoleService) {
        this.adminRoleService = adminRoleService;
        this.adminPermissionService = adminPermissionService;
        this.adminRolePermissionService = adminRolePermissionService;
        this.redisTemplate = redisTemplate;
        this.adminUserRoleService = adminUserRoleService;
    }

    private Object validate(Role role) {
        String name = role.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }

    @ApiOperation("获得角色列表")
    @GetMapping("/list")
    public Object list(){
        return ResponseUtil.ok(adminRoleService.listRole());
    }

    @PostMapping("/update")
    public Object update(@RequestBody Role role) {

        adminRoleService.updateRole(role);
        return ResponseUtil.ok();
    }


    @GetMapping("/permissions")
    public Object getPermissions(Long roleId) {
        List<Permission> systemPermissions = adminPermissionService.getPermission();
        List<RolePermission> assignedPermissions = adminRolePermissionService.getRolePermissions(roleId);

        Map<String, Object> data = new HashMap<>();
        data.put("systemPermissions", systemPermissions);
        data.put("assignedPermissions", assignedPermissions);
        return ResponseUtil.ok(data);
    }

    @PostMapping("/permissions")
    @Transactional
    public Object updatePermissions(@RequestBody RolePermissionDto rolePermissionDto){
        rolePermissionDto.getPermissions();
        Long roleId = rolePermissionDto.getRoleId();
        log.info("{}", roleId);
        if(roleId == 1391338689157787649L){
            return ResponseUtil.fail(ResponseCode.ROLE_SUPER_SUPERMISSION, "当前角色的超级权限不能变更");
        }

        for (Long permissionId:rolePermissionDto.getPermissions()) {
            adminRolePermissionService.addRolePermission(roleId,permissionId);
        }
        List<Long> userIdList = adminUserRoleService.getUserIdListByRoleId(roleId);
        for(Long userId: userIdList){

            log.info("删除缓存: {}", "permission:userId::" + userId);
            redisTemplate.delete("permission:userId::" + userId);

        }
        // 先删除旧的权限，再更新新的权限
        adminRolePermissionService.deletePermission(roleId);
        for (Long permissionId:rolePermissionDto.getPermissions()) {
            adminRolePermissionService.addRolePermission(roleId,permissionId);
        }
        return ResponseUtil.ok();
    }


}
